package com.wuyan.locationdog.help

import android.app.Activity
import android.graphics.Bitmap
import android.view.WindowManager
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.wuyan.locationdog.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import java.io.File
import java.io.FileOutputStream
import java.io.IOException


/**
 * 完成刷新和加载更多(简写) currentPage从0开始的
 */
fun finishRefreshAndLoadMoreNoMoreData(smartRefreshLayout: SmartRefreshLayout?, currentPage: Int, total: Int) {
    smartRefreshLayout?.apply {
        finishRefresh()
        finishLoadMore()
        if (currentPage >= total) {
            finishLoadMoreWithNoMoreData()
            return
        }
    }
}


/**
 * 改变背景颜色
 */
fun darkenBackground(activity: Activity, bgColor: Float) {
    val lp: WindowManager.LayoutParams = activity.window.getAttributes()
    lp.alpha = bgColor
    activity.window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
    activity.window.attributes = lp
}


fun countDownCoroutines(
    total: Int,
    scope: CoroutineScope,
    onTick: ((Int) -> Unit)? = null,
    onStart: (() -> Unit)? = null,
    onFinish: (() -> Unit)? = null,
): Job {
    return flow {
        for (i in total downTo 0) {
            emit(i)
            delay(1000)
        }
    }.flowOn(Dispatchers.Main)
        .onStart { onStart?.invoke() }
        .onCompletion { onFinish?.invoke() }
        .onEach { onTick?.invoke(it) }
        .launchIn(scope)
}


fun saveBitmapToFile(path: String, bm: Bitmap, picName: String) {
    val f = File(path, picName)
    var out: FileOutputStream? = null
    try {
        out = FileOutputStream(f)
        bm.compress(Bitmap.CompressFormat.PNG, 90, out)
        out.flush()
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        try {
            out?.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}

fun gpsSignalText(gpsSignal: Int): String {
    return when (gpsSignal) {
        5 -> "信号极强"
        4 -> "信号良好"
        3 -> "信号中等"
        2 -> "信号微弱"
        else -> {
            "信号无"
        }
    }
}

fun gpsSignalImg(gpsSignal: Int): Int {
    return when (gpsSignal) {
        5 -> R.mipmap.xinhao5
        4 -> R.mipmap.xinhao4
        3 -> R.mipmap.xinhao3
        2 -> R.mipmap.xinhao2
        else -> {
            R.mipmap.xinhao1
        }
    }
}

fun powerImg(gpsSignal: Int): Int {
    return when (gpsSignal) {
        in 81..100 -> R.mipmap.dianchi5
        in 61..80 -> R.mipmap.dianchi4
        in 41..60 -> R.mipmap.dianchi3
        in 21..40 -> R.mipmap.dianchi2
        else -> {
            R.mipmap.dianchi1
        }
    }
}

fun directRotation(direct: String?): Int {
    direct?.apply {
        if (direct.contains("东北")) {
            return 45
        } else if (direct.contains("东南")) {
            return 135
        } else if (direct.contains("西南")) {
            return 225
        } else if (direct.contains("西北")) {
            return 315
        } else if (direct.contains("北")) {
            return 0
        } else if (direct.contains("南")) {
            return 180
        } else if (direct.contains("东")) {
            return 90
        } else if (direct.contains("西")) {
            return 270
        } else {
            return 0

        }
    }
    return 0
}

fun workOrderTypeNum(workOrderType: String): Int {
    //反馈类型(1问题反馈 2产品建议 3设备问题 4系统问题 5续费问题 6设备维修 7售后问题 8其他问题)
    return when (workOrderType) {
        "问题反馈" -> 1
        "产品建议" -> 2
        "设备问题" -> 3
        "系统问题" -> 4
        "续费问题" -> 5
        "设备维修" -> 6
        "售后问题" -> 7

        else -> {
            8
        }
    }
}

fun getKmOrM(distance: Int): String {
    if (distance < 1000) {
        return distance.toString() + "米"
    }
    return (distance.toFloat() / 1000).toString() + "km"
}
